package com.lyd.office.service.impl;

import cn.hutool.core.util.RuntimeUtil;
import com.lyd.office.service.FileService;
import com.lyd.office.utils.ProcessUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * 描述 文件服务实现
 *
 * @author liyadong
 * @date 2024-05-10 15:01
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {
    /**
     *  程序位置（linux 可以直接 soffice 替换 ）
     */
    private static final String COMMAND_PATH = "/Applications/LibreOffice.app/Contents/MacOS/soffice ";
    @Override
    public void htmlToWord(String htmlPath, String wordPath) {
        String command = COMMAND_PATH + " --headless --convert-to docx:\"MS Word 2007 XML\" "  + htmlPath + " --outdir " + wordPath;
        log.info("执行的指令：{}", command);
        Integer exec = ProcessUtils.exec(command);
        log.info("返回代码：{}", exec);
    }

    @Override
    public void wordToPdf(String wordFilePath, String pdfFilePath) {
        String command = COMMAND_PATH + " --headless --convert-to pdf:writer_pdf_Export --outdir " + pdfFilePath + " " + wordFilePath;
        log.info("执行的指令：{}", command);
        Integer execResult = ProcessUtils.exec(command);
        log.info("返回代码:{}", execResult);
    }
}
